package org.jeecg.modules.tt_question_collect.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.tt_question_collect.entity.TtPlanConfirm;
import org.jeecg.modules.tt_question_collect.entity.TtQuestionCollect;
import org.jeecg.modules.tt_question_collect.entity.TtQuestionFacj;
import org.jeecg.modules.tt_question_collect.entity.wtexport;
import org.jeecg.modules.tt_question_collect.service.ITtPlanConfirmService;
import org.jeecg.modules.tt_question_collect.service.ITtQuestionCollectService;
import org.jeecg.modules.tt_question_collect.service.IWtexportService;
import org.jeecg.modules.tt_question_collect.service.impl.TtQuestionCollectServiceImpl;
import org.jeecg.modules.tt_question_collect.util.TransUntil;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @Description: 出具方案表
* @Author: jeecg-boot
* @Date:   2021-03-09
* @Version: V1.0
*/
@Api(tags="出具方案表")
@RestController
@RequestMapping("/tt_question_collect/xqexport")
@Slf4j
public class ExportXqController extends JeecgController<wtexport, IWtexportService> {
   @Autowired
   private IWtexportService IWtexportService;

    @Autowired
    private ITtQuestionCollectService ttquestioncollectService;






    @AutoLog(value = "需求问题通过导出列表")
    @ApiOperation(value="需求问题通过导出列表", notes="需求问题通过导出列表")
    @GetMapping(value = "/xqwtexportlist")
    public Result<?> jiraTbPageList(wtexport wtexport,
                                       @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                       @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                       HttpServletRequest req) {


        Page<wtexport> pagelist = new Page<wtexport>(pageNo, pageSize);
        pagelist = IWtexportService.getexportlist(pagelist,wtexport);
        return Result.OK(pagelist);
    }



    @AutoLog(value = "需求问题优先级导出排序列表")
    @ApiOperation(value="需求问题优先级导出排序列表", notes="需求问题优先级导出排序列表")
    @GetMapping(value = "/xqwtyxjdcpxlist")
    public Result<?> xqwtyxjdcpxPageList(wtexport wtexport,
                                    @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                    @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                    HttpServletRequest req) {


        Page<wtexport> pagelist = new Page<wtexport>(pageNo, pageSize);
        pagelist = IWtexportService.getyxjpxlist(pagelist,wtexport);
        return Result.OK(pagelist);
    }




    @AutoLog(value = "更新入导出库标记")
    @ApiOperation(value = "更新入导出库标记", notes = "更新入导出库标记")
    @PutMapping(value = "/updateyxjbj")
    public Result<?> updateYxjbj(@RequestBody TtQuestionCollect ttQuestionCollect ) {
        //
        IWtexportService.updateyxjbj(ttQuestionCollect.getId(),ttQuestionCollect.getHighestbj());

        return Result.OK();
    }





    /**
     * 导出excel
     *
     * @param request
     * @param ttPlanConfirm
     */
    @RequestMapping(value = "/exportxqtglist")
    public ModelAndView exportXqtglist(HttpServletRequest request, wtexport wteportt ) {
        System.out.println(wteportt.getFafj());


     //   Workbook wb = ExcelExportUtil.exportExcel(new ExportParams(null, "sheetName"),SysUser.class,ls);
        return super.exportXls(request, wteportt, wtexport.class, "需求收集");

    }


    @AutoLog(value = "首页查看待审批的个数")
    @ApiOperation(value = "首页查看待审批的个数", notes = "首页查看待审批的个数")
    @GetMapping(value = "/syckdspnum")
    public Result<?> syCkDspnum() {
        //查询
        String num = IWtexportService.getsyckdspnum();
        return Result.OK(num);
    }





    @AutoLog(value = "查询统计各部门柱状图数据")
    @ApiOperation(value = "查询统计各部门柱状图数据", notes = "查询统计各部门柱状图数据")
    @GetMapping(value = "/querycharbardata")
    public Result<?> queryCharBardata() {
        //查询
        List<Map<String,Object>> map = IWtexportService.getbmcode();
        return Result.OK(map);
    }

    @AutoLog(value = "查询统计各审批人审批数据")
    @ApiOperation(value = "查询统计各审批人审批数据", notes = "查询统计各审批人审批数据")
    @GetMapping(value = "/querytjsprdata")
    public Result<?> queryTjSprData() {
        //查询
        List<Map<String,Object>> map = IWtexportService.getsprdata();
        return Result.OK(map);
    }



    @AutoLog(value = "查询统计各问题累计用时-分页列表查询")
    @ApiOperation(value = "查询统计各问题累计用时-分页列表查询", notes = "查询统计各问题累计用时-分页列表查询")
    @RequestMapping(value = "/ljtimelist", method = RequestMethod.GET)
    public Result<Page<TtQuestionCollect>> queryWtljtimePageList(TtQuestionCollect ttQuestionCollect,
                                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) throws ParseException {
        Result<Page<TtQuestionCollect>> result = new Result<Page<TtQuestionCollect>>();

        Page<TtQuestionCollect> pageList = new Page<TtQuestionCollect>(pageNo, pageSize);
        pageList = IWtexportService.queryljtime(pageList,ttQuestionCollect);
        result.setSuccess(true);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (int i = 0; i <pageList.getRecords().size() ; i++) {
            if ("完成".equals(pageList.getRecords().get(i).getDealstatus())){
              //  System.out.println("已完成");
                long diff = pageList.getRecords().get(i).getUpdateTime().getTime() - pageList.getRecords().get(i).getCreateTime().getTime();//这样得到的差值是毫秒级别
                long days = diff / (1000 * 60 * 60 * 24);
                long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
                long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
                pageList.getRecords().get(i).setBy1(days+"天"+hours+"小时"+minutes+"分");
              //  System.out.println(""+days+"天"+hours+"小时"+minutes+"分");
                System.out.println(""+days+"天"+hours+"小时"+minutes+"分"+"============"+"日期"+diff);
                pageList.getRecords().get(i).setBy2(diff+"");
            }else {
                Date d = new Date();
                String dateNowStr = sdf.format(d);
                Date endDate = sdf.parse(dateNowStr);
                long diff = endDate.getTime() - pageList.getRecords().get(i).getCreateTime().getTime();//这样得到的差值是毫秒级别
                long days = diff / (1000 * 60 * 60 * 24);
                long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
                long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
                System.out.println(""+days+"天"+hours+"小时"+minutes+"分");
                pageList.getRecords().get(i).setBy1(days+"天"+hours+"小时"+minutes+"分");
               // System.out.println("未完成");
                System.out.println(""+days+"天"+hours+"小时"+minutes+"分"+"============"+"日期"+diff);
                pageList.getRecords().get(i).setBy2(diff+"");
            }
        }

        result.setResult(pageList);
        return result;
    }


}
